package com.yunnuo.system.dict.service.impl;

import com.yunnuo.system.comm.util.page.TableDataInfo;
import com.yunnuo.system.dict.dao.IDictDataDao;
import com.yunnuo.system.dict.entity.DictData;
import com.yunnuo.system.dict.service.IDictDataService;
import com.yunnuo.system.users.entity.PageParams;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by w_weihf on 2018/8/3.
 */
@Service
public class DictDataServiceImpl implements IDictDataService {
    @Autowired
    private IDictDataDao dictDataDao;
    @Override
    public void addVO(DictData vo) throws ServiceException {
        dictDataDao.save(vo);
    }

    @Override
    public void updateVO(DictData vo) throws ServiceException {
        dictDataDao.saveAndFlush(vo);
    }

    @Override
    public void  delList(String[] ids) throws ServiceException {

    }

    @Override
    public void deleteVo(String id) throws ServiceException {
        dictDataDao.delete(id);
    }

    @Override
    public List<DictData> findList() throws ServiceException {
        return dictDataDao.findAll();
    }


    @Override
    public List<?> findList(DictData vo, String other) throws ServiceException {
        return null;
    }


    @Override
    public DictData findVO(String id) throws ServiceException {
        return dictDataDao.findOne(id);
    }

    @Override
    public DictData selectOneByDictCode(String dictCode) {

        List list =dictDataDao.findAllByDictCode(dictCode);
        if(list.size()>0){
            return (DictData) list.get(0);
        }else{
            return null;
        }
    }

    @Override
    public List selectAllByType(String dictType) {
        return dictDataDao.findAllByDictType(dictType);
    }
    /**
     * 根据主键查询
     * @param id
     * @return
     */
    @Override
    public DictData selectDictDataById(String id) {
        return dictDataDao.findOne(id);
    }

    /**
     * 分页查询
     * @param pageParams
     * @param pageRequest
     * @return
     */
    @Override
    public TableDataInfo selectConfigListByPageParams(PageParams pageParams, PageRequest pageRequest, String dictType) {
        TableDataInfo dataInfo=new TableDataInfo();
        long total=0;
        List<DictData>  list=new ArrayList<>();
        if ("".equals(pageParams.getSearchValue())){
            list=dictDataDao.findAllByDictType(dictType,pageRequest).getContent();
            total=dictDataDao.count();
        }else if (!"".equals(pageParams.getSearchValue()) && "".equals(dictType)){
            list=dictDataDao.findAllByDictCode(pageParams.getSearchValue(),pageRequest).getContent();
            total=dictDataDao.countAllByDictCode(pageParams.getSearchValue());
        }else if ("".equals(pageParams.getSearchValue()) && !"".equals(dictType)){
            list=dictDataDao.findAllByDictType(dictType,pageRequest).getContent();
            total=dictDataDao.countAllByDictType(dictType);
        }else {
            list=dictDataDao.findAllByDictTypeAndDictCode(dictType,pageParams.getSearchValue(),pageRequest).getContent();
            total=dictDataDao.countAllByDictTypeAndDictCode(dictType,pageParams.getSearchValue());
        }

        dataInfo.setRows(list);
        dataInfo.setTotal(total);
        return dataInfo;
    }

    /**
     * 保存或修改
     * @param dict
     */
    @Override
    public void save(DictData dict) {
        dict=dictDataDao.save(dict);
    }
}
